/*
 * LinearAlloc.h
 *
 *  Created on: 2011-7-9
 *      Author: gexiao
 */

/*
 * Simple linear memory allocator
 */
#ifndef LINEARALLOC_H_
#define LINEARALLOC_H_
#include "oo/Object.h"
#include <pthread.h>
/*
 * Linear allocation state. We could tuck this into start of the
 * allocated region,but that would prevent us from sharing the rest
 * of that first page.
 */
typedef struct LinearAllocHdr {
	int curOffset; //offset where next data goes
	pthread_mutex_t lock; //controls updates to this struct

	char* mapAddr; //start of mmap()ed region
	int mapLength; //length of region
	int firstOffset; //for chasing through

	short* writeRefCount; //for ENFORCE_READ_ONLY
} LinearAllocHdr;

/*
 * Create a new alloc region.
 */
LinearAllocHdr* dvmLinearAllocCreate(Object* classLoader);

/*
 * Allocate a chunk of memory. The memory will be zeroed out.
 *
 * For ENFORCE_READ_ONLY, call dvmLinearReadOnly on the result.
 */
void* dvmLinearAlloc(Object* classLoader, size_t size);

/*
 * Reallocate a chunk.  The original storage is not released, but may be
 * erased to aid debugging.
 *
 * For ENFORCE_READ_ONLY, call dvmLinearReadOnly on the result.  Also, the
 * caller should probably mark the "mem" argument read-only before calling.
 */
void* dvmLinearRealloc(Object* classLoader, void* mem, size_t newSize);

/*
 * Mark a chunk of memory from Alloc or Realloc as read-only.  This must
 * be done after all changes to the block of memory have been made. This
 * actually operates on a page granularity.
 */
INLINE void dvmLinearReadOnly(Object* classLoader, void* mem) {

}

/*
 * Make a chunk of memory writable again.
 */
INLINE void dvmLinearReadWrite(Object* classLoader, void* mem) {

}

/*
 * Free a chunk.  Does not increase available storage, but the freed area
 * may be erased to aid debugging.
 */
void dvmLinearFree(Object* classLoader, void* mem);
#endif /* LINEARALLOC_H_ */
